package ${model.packageFullName};

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import ace.cmp.jmockdata.core.impl.util.JMockDataUtils;
import ace.cmp.security.oauth2.resource.server.test.service.MockTokenIntrospectService;
import ace.cmp.util.AceAssertUtils;
import ${model.apiClassFullName};
import ${model.baseTestClassFullName};
import ${model.dtoClassFullName};

/**
* ${model.entityRemark} api unit test.
*
* @author Caspar
*/
@Slf4j
@AllArgsConstructor
public class ${model.className} extends BaseTest {
    private final ${model.apiClassName} api;
    private final JMockDataUtils jMockDataUtils;
    private final MockTokenIntrospectService mockTokenIntrospectService;

	  @Test
    public void test_save() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        model.setId(null);
        model.setRowVersion(null);
        ${model.entityIdType} id = api.save(model).check();
        ${model.dtoClassName} queryModel = api.getById(id).check();
        AceAssertUtils.isNull(queryModel);
        ${model.entityIdType} id2 = api.save(queryModel).check();
        AceAssertUtils.isFalse(id.equals(id2));
    }

    @Test
    public void test_saveNotNullField() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        model.setId(null);
        model.setRowVersion(null);
        ${model.entityIdType} id = api.saveNotNullField(model).check();
        ${model.dtoClassName} queryModel = api.getById(id).check();
        AceAssertUtils.isNull(queryModel);
        //queryModel.setRowVersion(null);
        ${model.entityIdType} id2 = api.saveNotNullField(queryModel).check();
        AceAssertUtils.isFalse(id.equals(id2));
    }

    @Test
    public void test_add() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        ${model.entityIdType} id = api.add(model).check();
        ${model.dtoClassName} queryModel = api.getById(id).check();
        AceAssertUtils.isNull(queryModel);
    }

    @Test
    public void test_addNotNullField() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        ${model.entityIdType} id = api.addNotNullField(model).check();
        ${model.dtoClassName} queryModel = api.getById(id).check();
        AceAssertUtils.isNull(queryModel);
    }

    @Test
    public void test_modify() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        ${model.entityIdType} id = api.add(model).check();
        ${model.dtoClassName} modifyModel = api.getById(id).check();
        modifyModel.setUpdateTime(System.currentTimeMillis());
        api.modify(modifyModel).check();
    }

    @Test
    public void test_modifyNotNullField() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        ${model.entityIdType} id = api.add(model).check();
        ${model.dtoClassName} modifyModel = api.getById(id).check();
        modifyModel.setUpdateTime(System.currentTimeMillis());
        api.modifyNotNullField(modifyModel).check();
    }

    @Test
    public void test_remove() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        ${model.entityIdType} id = api.add(model).check();
        api.remove(Arrays.asList(id)).check();
        ${model.dtoClassName} modifyModel = api.getById(id).check();
        AceAssertUtils.isNotNull(modifyModel);
    }

    @Test
    public void test_removeLogic() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);
        ${model.entityIdType} id = api.add(model).check();
        api.removeLogic(Arrays.asList(id)).check();
        ${model.dtoClassName} modifyModel = api.getById(id).check();
        AceAssertUtils.isTrue(modifyModel.getDeleteFlag().equals(0));
    }

    @Test
    public void test_getId() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);

        ${model.entityIdType} id = api.add(model).check();
        ${model.dtoClassName} queryModel = api.getById(id).check();
        AceAssertUtils.isNull(queryModel, "query fail");
    }

    @Test
    public void test_getListByIdIn() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);

        ${model.entityIdType} id = api.add(model).check();
        List<${model.dtoClassName}> queryModels = api.getListByIdIn(id).check();
        AceAssertUtils.isTrue(CollectionUtils.isEmpty(queryModels), "query fail");
    }

    @Test
    public void test_getMapByIdIn() {
        mockTokenIntrospectService.mockClient();
        ${model.dtoClassName} model = jMockDataUtils.mock(${model.dtoClassName}.class);

        ${model.entityIdType} id = api.add(model).check();
        Map<${model.entityIdType}, ${model.dtoClassName}> queryModels = api.getMapByIdIn(id).check();
        AceAssertUtils.isTrue(MapUtils.isEmpty(queryModels), "query fail");
    }
}
